home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PROGEDIT / H402.ZIP / ELV17SRC.ZIP / TINYTCAP.C < prev    next >
C/C++ Source or Header  |  1992-12-30  |  11KB  |  393 lines

  1. /* tinytcap.c */
  2.  
  3. /* This file contains functions which simulate the termcap functions.
  4.  *
  5.  * It doesn't access a "termcap" file.  Instead, it uses an initialized array
  6.  * of strings to store the entries.  Any string that doesn't start with a ':'
  7.  * is taken to be the name of a type of terminal.  Any string that does start
  8.  * with a ':' is interpretted as the list of fields describing all of the
  9.  * terminal types that precede it.
  10.  *
  11.  * Note: since these are C strings, you can't use special sequences like
  12.  * ^M or \E in the fields; your C compiler won't understand them.  Also,
  13.  * at run time there is no way to tell the difference between ':' and '\072'
  14.  * so I sure hope your terminal definition doesn't require a ':' character.
  15.  *
  16.  * getenv(TERM) on VMS checks the SET TERM device setting.  To implement
  17.  * non-standard terminals set the logical ELVIS_TERM in VMS. (jdc)
  18.  *
  19.  * Other possible terminal types are...
  20.  *     TERM_WYSE925    - "wyse925", a Wyse 50 terminal emulating Televideo 925
  21.  * ... or you could set $TERMCAP to the terminal's description string, which
  22.  * $TERM set up to match it.
  23.  *
  24.  * Note that you can include several terminal types at the same time.  Elvis
  25.  * chooses which entry to use at runtime, based primarily on the value of $TERM.
  26.  */
  27.  
  28.  
  29. #include "config.h"
  30. #ifdef __STDC__
  31. # include <stdlib.h>
  32. #else
  33. extern char *getenv();
  34. #endif
  35.  
  36. /* decide which terminal descriptions should *always* be included. */
  37. #if MSDOS
  38. # define    TERM_NANSI
  39. # define    TERM_DOSANSI
  40. # if RAINBOW
  41. #  define    TERM_RAINBOW
  42. # endif
  43. #endif
  44.  
  45. #if VMS
  46. # define    TERM_VT100
  47. # define    TERM_VT100W
  48. # define    TERM_VT52
  49. #endif
  50.  
  51. #if AMIGA
  52. # define    TERM_AMIGA    /* Internal Amiga termcap entry */
  53. /* # define    TERM_VT52    /* The rest of these are here for those */
  54. # define    TERM_VT100    /* people who want to use elvis over an */
  55. /* # define    TERM_NANSI    /* AUX: port (serial.device). */
  56. /* # define    TERM_DOSANSI    /* Take out all but AMIGA to save memory. */
  57. /* # define    TERM_MINIX    /* Vanilla ANSI? */
  58. /* # define    TERM_925    /* Hang a terminal off your Amiga */
  59. #endif
  60.  
  61. #if MINIX || UNIXV
  62. # define    TERM_MINIX
  63. #endif
  64.  
  65. #if COHERENT
  66. # define    TERM_COHERENT
  67. #endif
  68.  
  69. #if TOS
  70. # define    TERM_ATARI
  71. #endif
  72.  
  73. static char *termcap[] =
  74. {
  75. #ifdef TERM_AMIGA
  76. "AA",
  77. "amiga",
  78. "Amiga ANSI",
  79. /* Amiga termcap modified from version 1.3 by Kent Polk */
  80. ":co#80:li#24:am:bs:bw:xn:\
  81. :AL=\233%dL:DC=\233%dP:DL=\233%dM:DO=\233%dB:\
  82. :LE=\233%dD:RI=\233%dC:SF=\233%dS:SR=\233%dT:UP=\233%dA:IC=\233%d@:\
  83. :ae=\2330m:al=\233L:as=\2333m:bl=\007:bt=\233Z:cd=\233J:\
  84. :ce=\233K:cl=\013:cm=\233%i%d;%dH:dc=\233P:dl=\233M:do=\233B:\
  85. :kb=^H:ho=\233H:ic=\233@:is=\23320l:\
  86. :mb=\2337;2m:md=\2331m:me=\2330m:mh=\2332m:mk=\2338m:mr=\2337m:nd=\233C:\
  87. :rs=\033c:se=\2330m:sf=\233S:so=\2337m:sb=\233T:sr=\233T:ue=\23323m:\
  88. :up=\233A:us=\2334m:vb=\007:ve=\233\040p:vi=\2330\040p:\
  89. :k1=\2330~:k2=\2331~:k3=\2332~:k4=\2333~:k5=\2334~:\
  90. :k6=\2335~:k7=\2336~:k8=\2337~:k9=\2338~:k0=\2339~:\
  91. :s1=\23310~:s2=\23311~:s3=\23312~:s4=\23313~:s5=\23314~:\
  92. :s6=\23315~:s7=\23316~:s8=\23317~:s9=\23318~:s0=\23319~:\
  93. :kd=\233B:kl=\233D:kn#10:kr=\233C:ku=\233A:le=\233D:\
  94. :kP=\233T:kN=\233S:kh=\233\040A:kH=\233\040@:",
  95. #endif
  96.  
  97. #ifdef TERM_NANSI
  98. "fansi",
  99. "nnansi",
  100. "nansi",
  101. "pcbios",
  102. ":al=\033[L:dl=\033[M:am:bs:ce=\033[K:cl=\033[2J:\
  103. :cm=\033[%i%d;%dH:co#80:do=\033[B:\
  104. :k1=#;:k2=#<:k3=#=:k4=#>:k5=#?:k6=#@:k7=#A:k8=#B:k9=#C:k0=#D:\
  105. :s1=#T:s2=#U:s3=#V:s4=#W:s5=#X:s6=#Y:s7=#Z:s8=#[:s9=#\\:s0=#]:\
  106. :c1=#^:c2=#_:c3=#`:c4=#a:c5=#b:c6=#c:c7=#d:c8=#e:c9=#f:c0=#g:\
  107. :a1=#h:a2=#i:a3=#j:a4=#k:a5=#l:a6=#m:a7=#n:a8=#o:a9=#p:a0=#q:\
  108. :kd=#P:kh=#G:kH=#O:kI=#R:kl=#K:kN=#Q:kP=#I:kr=#M:ku=#H:\
  109. :li#25:md=\033[1m:me=\033[m:nd=\033[C:se=\033[m:so=\033[7m:\
  110. :ue=\033[m:up=\033[A:us=\033[4m:",
  111. #endif
  112.  
  113. #ifdef TERM_DOSANSI
  114. #if !ANY_UNIX
  115. "ansi",
  116. #endif
  117. "dosansi",
  118. ":am:bs:ce=\033[K:cl=\033[2J:\
  119. :cm=\033[%i%d;%dH:co#80:do=\033[B:\
  120. :k1=#;:k2=#<:k3=#=:k4=#>:k5=#?:k6=#@:k7=#A:k8=#B:k9=#C:k0=#D:\
  121. :s1=#T:s2=#U:s3=#V:s4=#W:s5=#X:s6=#Y:s7=#Z:s8=#[:s9=#\\:s0=#]:\
  122. :c1=#^:c2=#_:c3=#`:c4=#a:c5=#b:c6=#c:c7=#d:c8=#e:c9=#f:c0=#g:\
  123. :a1=#h:a2=#i:a3=#j:a4=#k:a5=#l:a6=#m:a7=#n:a8=#o:a9=#p:a0=#q:\
  124. :kd=#P:kh=#G:kH=#O:kI=#R:kl=#K:kN=#Q:kP=#I:kr=#M:ku=#H:\
  125. :li#25:md=\033[1m:me=\033[m:nd=\033[C:se=\033[m:so=\033[7m:\
  126. :ue=\033[m:up=\033[A:us=\033[4m:",
  127. #endif
  128.  
  129. #ifdef TERM_RAINBOW
  130. "vt220",
  131. "rainbow",
  132. ":al=\033[L:dl=\033[M:am:bs:ce=\033[K:cl=\033[2J:\
  133. :cm=\033[%i%d;%dH:co#80:do=\033[B:kd=\033[B:kl=\033[D:\
  134. :kr=\033[C:ku=\033[A:kP=\033[5~:kN=\033[6~:kI=\033[2~:\
  135. :li#24:md=\033[1m:me=\033[m:nd=\033[C:se=\033[m:so=\033[7m:\
  136. :ue=\033[m:up=\033[A:us=\033[4m:xn:",
  137. #endif
  138.  
  139. #ifdef TERM_VT100
  140. "vt100-80",
  141. "vt200-80",
  142. "vt300-80",
  143. "vt101-80",
  144. "vt102-80",
  145. ":al=\033[L:am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  146. :co#80:dl=\033[M:do=\033[B:k0=\033[20~:k1=\033[1~:\
  147. :k2=\033[2~:k3=\033[3~:k4=\033[4~:k5=\033[5~:k6=\033[6~:\
  148. :k7=\033[17~:k8=\033[18~:k9=\033[19~:kd=\033[B:kh=\033[H:\
  149. :kH=\033[Y:kI=\033[I:kl=\033[D:kN=\033[U:kP=\033[V:\
  150. :kr=\033[C:ku=\033[A:li#24:md=\033[1m:me=\033[m:nd=\033[C:\
  151. :se=\033[m:so=\033[7m:ti=\033[1;24r\033[24;1H:\
  152. :ue=\033[m:up=\033[A:us=\033[4m:xn:",
  153. #endif
  154.  
  155. #ifdef TERM_VT100W
  156. "vt100-w",
  157. "vt200-w",
  158. "vt300-w",
  159. "vt101-w",
  160. "vt102-w",
  161. "vt100-132",
  162. "vt200-132",
  163. "vt300-132",
  164. "vt101-132",
  165. "vt102-132",
  166. ":al=\033[L:am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  167. :co#132:dl=\033[M:do=\033[B:k0=\033[20~:k1=\033[1~:\
  168. :k2=\033[2~:k3=\033[3~:k4=\033[4~:k5=\033[5~:k6=\033[6~:\
  169. :k7=\033[17~:k8=\033[18~:k9=\033[19~:kd=\033[B:kh=\033[H:\
  170. :kH=\033[Y:kI=\033[I:kl=\033[D:kN=\033[U:kP=\033[V:\
  171. :kr=\033[C:ku=\033[A:li#24:md=\033[1m:me=\033[m:nd=\033[C:\
  172. :se=\033[m:so=\033[7m:ti=\033[1;24r\033[24;1H:\
  173. :ue=\033[m:up=\033[A:us=\033[4m:xn:",
  174. #endif
  175.  
  176. #ifdef TERM_VT52
  177. "vt52",
  178. ":do=\n:le=\b:up=\033A:nd=\033C:cm=\033Y%+ %+ :ti=\033e\033v:\
  179. :sr=\033I:cd=\033J:ce=\033K:cl=\033H\033J:co#80:li#24:\
  180. :ku=\033A:kd=\033B:kr=\033C:kl=\033D:kb=\b:pt:am:xn:bs:",
  181. #endif
  182.  
  183. #ifdef TERM_MINIX
  184. "minix",
  185. "ansi",
  186. "AT386",
  187. ":al=\033[L:am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  188. :co#80:dl=\033[M:do=\033[B:k0=\033[20~:k1=\033[1~:\
  189. :k2=\033[2~:k3=\033[3~:k4=\033[4~:k5=\033[5~:k6=\033[6~:\
  190. :k7=\033[17~:k8=\033[18~:k9=\033[19~:kd=\033[B:kh=\033[H:\
  191. :kH=\033[Y:kI=\033[I:kl=\033[D:kN=\033[U:kP=\033[V:\
  192. :kr=\033[C:ku=\033[A:li#25:md=\033[1m:me=\033[m:nd=\033[C:\
  193. :se=\033[m:so=\033[7m:ue=\033[m:up=\033[A:us=\033[4m:",
  194. #endif /* MINIX */
  195.  
  196. #ifdef TERM_COHERENT
  197. "coherent",
  198. "ansipc",
  199. ":al=\033[L:am:bs:ce=\033[K:cl=\033[2J:cm=\033[%i%d;%dH:\
  200. :co#80:dl=\033[M:do=\033[B:k0=\033[0x:k1=\033[1x:k2=\033[2x:\
  201. :k3=\033[3x:k4=\033[4x:k5=\033[5x:k6=\033[6x:\
  202. :k7=\033[7x:k8=\033[8x:k9=\033[9x:kd=\033[B:kh=\033[H:\
  203. :kH=\033[24H:kI=\033[@:kl=\033[D:kN=\033[U:kP=\033[V:\
  204. :kr=\033[C:ku=\033[A:li#24:md=\033[1m:me=\033[m:\
  205. :nd=\033[C:se=\033[m:so=\033[7m:ue=\033[m:up=\033[A:\
  206. :us=\033[4m:",
  207. #endif /* COHERENT */
  208.  
  209. #ifdef TERM_ATARI
  210. "atari-st",
  211. "vt52",
  212. ":al=\033L:am:bs:ce=\033K:cl=\033E:cm=\033Y%i%+ %+ :\
  213. :co#80:dl=\033M:do=\033B:\
  214. :k1=#;:k2=#<:k3=#=:k4=#>:k5=#?:k6=#@:k7=#A:k8=#B:k9=#C:k0=#D:\
  215. :s1=#T:s2=#U:s3=#V:s4=#W:s5=#X:s6=#Y:s7=#Z:s8=#[:s9=#\\:s0=#]:\
  216. :c1=#^:c2=#_:c3=#`:c4=#a:c5=#b:c6=#c:c7=#d:c8=#e:c9=#f:c0=#g:\
  217. :a1=#h:a2=#i:a3=#j:a4=#k:a5=#l:a6=#m:a7=#n:a8=#o:a9=#p:a0=#q:\
  218. kd=#P:kh=#G:kI=#R:kl=#K:kr=#M:ku=#H:li#25:nd=\033C:se=\033q:\
  219. :so=\033p:te=:ti=\033e\033v:up=\033A:",
  220. #endif
  221.  
  222. #ifdef TERM_925
  223. "wyse925",
  224. ":xn@:\
  225. :hs:am:bs:co#80:li#24:cm=\033=%+ %+ :cl=\033*:cd=\033y:\
  226. :ce=\033t:is=\033l\033\":\
  227. :al=\033E:dl=\033R:im=:ei=:ic=\033Q:dc=\033W:\
  228. :ho=\036:nd=\014:bt=\033I:pt:so=\033G4:se=\033G0:sg#1:us=\033G8:ue=\033G0:ug#1:\
  229. :up=\013:do=\026:kb=\010:ku=\013:kd=\026:kl=\010:kr=\014:\
  230. :kh=\036:ma=\026\012\014 :\
  231. :k1=\001@\r:k2=\001A\r:k3=\001B\r:k4=\001C\r:k5=\001D\r:k6=\001E\r:k7=\001F\r:\
  232. :k8=\001G\r:k9=\001H\r:k0=\001I\r:ko=ic,dc,al,dl,cl,ce,cd,bt:\
  233. :ts=\033f:fs=\033g:ds=\033h:sr=\033j:",  /* was :xn: for tvi925 alone*/
  234. #endif
  235.  
  236. (char *)0
  237. };
  238.  
  239.  
  240. static char *fields;
  241.  
  242.  
  243. /*ARGSUSED*/
  244. int tgetent(bp, name)
  245.     char    *bp;    /* buffer for storing the entry -- ignored */
  246.     char    *name;    /* name of the entry */
  247. {
  248.     int    i;
  249.  
  250.     /* if TERMCAP is defined, and seems to match, then use it */
  251.     fields = getenv("TERMCAP");
  252.     if (fields)
  253.     {
  254.         for (i = 0; fields[i] && fields[i] != ':'; i++)
  255.         {
  256.             if (!strncmp(fields + i, name, strlen(name)))
  257.             {
  258.                 return 1;
  259.             }
  260.         }
  261.     }
  262.  
  263.     /* locate the entry in termcap[] */
  264.     for (i = 0; termcap[i] && strcmp(termcap[i], name); i++)
  265.     {
  266.     }
  267.     if (!termcap[i])
  268.     {
  269.         return 0;
  270.     }
  271.  
  272.     /* search forward for fields */
  273.     while (termcap[i][0] != ':')
  274.     {
  275.         i++;
  276.     }
  277.     fields = termcap[i];
  278.     return 1;
  279. }
  280.  
  281.  
  282. static char *find(id, vtype)
  283.     char    *id;    /* name of a value to locate */
  284.     int    vtype;    /* '=' for strings, '#' for numbers, or 0 for bools */
  285. {
  286.     int    i;
  287.  
  288.     /* search for a ':' followed by the two-letter id */
  289.     for (i = 0; fields[i]; i++)
  290.     {
  291.         if (fields[i] == ':'
  292.          && fields[i + 1] == id[0]
  293.          && fields[i + 2] == id[1])
  294.         {
  295.             /* if correct type, then return its value */
  296.             if (fields[i + 3] == vtype)
  297.                 return &fields[i + 4];
  298.             else
  299.                 return (char *)0;
  300.         }
  301.     }
  302.     return (char *)0;
  303. }
  304.  
  305. int tgetnum(id)
  306.     char    *id;
  307. {
  308.     id = find(id, '#');
  309.     if (id)
  310.     {
  311.         return atoi(id);
  312.     }
  313.     return -1;
  314. }
  315.  
  316. int tgetflag(id)
  317.     char    *id;
  318. {
  319.     if (find(id, ':'))
  320.     {
  321.         return 1;
  322.     }
  323.     return 0;
  324. }
  325.  
  326. /*ARGSUSED*/
  327. char *tgetstr(id, bp)
  328.     char    *id;
  329.     char    **bp;    /* pointer to pointer to buffer - ignored */
  330. {
  331.     char    *cpy;
  332.  
  333.     /* find the string */
  334.     id = find(id, '=');
  335.     if (!id)
  336.     {
  337.         return (char *)0;
  338.     }
  339.  
  340.     /* copy it into the buffer, and terminate it with NUL */
  341.     for (cpy = *bp; *id != ':'; )
  342.     {
  343.         if (id[0] == '\\' && id[1] == 'E')
  344.             *cpy++ = '\033', id += 2;
  345.         else
  346.             *cpy++ = *id++;
  347.     }
  348.     *cpy++ = '\0';
  349.  
  350.     /* update the bp pointer */
  351.     id = *bp;
  352.     *bp = cpy;
  353.  
  354.     /* return a pointer to the copy of the string */
  355.     return id;
  356. }
  357.  
  358. /*ARGSUSED*/
  359. char *tgoto(cm, destcol, destrow)
  360.     char    *cm;    /* cursor movement string -- ignored */
  361.     int    destcol;/* destination column, 0 - 79 */
  362.     int    destrow;/* destination row, 0 - 24 */
  363. {
  364.     static char buf[30];
  365.  
  366. #ifdef CRUNCH
  367. # if TOS
  368.     sprintf(buf, "\033Y%c%c", ' ' + destrow, ' ' + destcol);
  369. # else
  370.     sprintf(buf, "\033[%d;%dH", destrow + 1, destcol + 1);
  371. # endif
  372. #else
  373.     if (cm[1] == 'Y' || cm[1] == '=')
  374.         sprintf(buf, "\033%c%c%c", cm[1], ' ' + destrow, ' ' + destcol);
  375.     else
  376.         sprintf(buf, "\033[%d;%dH", destrow + 1, destcol + 1);
  377. #endif
  378.     return buf;
  379. }
  380.  
  381. /*ARGSUSED*/
  382. void tputs(cp, affcnt, outfn)
  383.     char    *cp;        /* the string to output */
  384.     int    affcnt;        /* number of affected lines -- ignored */
  385.     int    (*outfn)();    /* the output function */
  386. {
  387.     while (*cp)
  388.     {
  389.         (*outfn)(*cp);
  390.         cp++;
  391.     }
  392. }
  393.